#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys

sys.path.append(
    os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "imlfapi")
)

from db import EntityTB, UsersTB


def main():
    # 查出所有entity
    entity_lst = EntityTB.find()
    # 循环所有entity
    for num, entity in enumerate(entity_lst):
        print("%s / %s" % (num, len(entity_lst)))
        # 在user表中查出userid类型
        user = UsersTB.find_one(username=entity["username"])
        # 更新entity的userid字段
        if user:
            EntityTB.set(userid=user["id"]).where(id=entity["id"])
            print("success:%s" % entity["id"])


if __name__ == "__main__":
    main()
